﻿
Imports System.Net
Imports System.Net.Sockets

Public Module netutils
    Public Function remoteEndPoint(ByVal tcpclient As TcpClient) As String
        Dim rtn As String = Nothing
        Try
            rtn = tcpclient.Client.RemoteEndPoint.ToString()
        Catch
            rtn = "#CANNOT_GET_REMOTE_ENDPOINT#"
        End Try

        Return rtn
    End Function

    Public Function localEndPoint(ByVal tcpclient As TcpClient) As String
        Dim rtn As String = Nothing
        Try
            rtn = tcpclient.Client.LocalEndPoint.ToString()
        Catch
            rtn = "#CANNOT_GET_LOCAL_ENDPOINT#"
        End Try

        Return rtn
    End Function

    Public Function displayHostPort(ByVal host As String, ByVal port As Int32) As String
        Return host + network.portMark + Convert.ToString(port)
    End Function

    Public Function supportIE6() As Boolean
        Return True
    End Function

    Public Function parseHostPort(ByVal address As String, ByRef host As String, ByRef port As Int32, _
                                  Optional ByVal separator As String = network.portMark) As Boolean
        Dim portStr As String = Nothing
        If strseg(address, host, portStr, separator, True) Then
            Return convertor.toint32(portStr, port, frombase:=10)
        Else
            Return False
        End If
    End Function

    Public Sub loosenSPMLimit(Optional ByVal connectionLimit As Int16 = 32)
        ServicePointManager.DefaultConnectionLimit = connectionLimit
#If Not (PocketPC OrElse Smartphone) Then
        ServicePointManager.EnableDnsRoundRobin = True
        ServicePointManager.UseNagleAlgorithm = False
        ServicePointManager.DnsRefreshTimeout <<= 4
#End If
    End Sub

#If Not PocketPC AndAlso Not Smartphone Then
    Public Function httpResponseLength(ByVal response As HttpListenerResponse, ByRef len As Int64) As Boolean
        Static sizeofStatus As Int64 = sizeof(Of Int32)()
        Try
            len = response.ContentLength64() + response.Headers.ToByteArray().LongLength() + _
                  encoding.utf8.GetByteCount(response.StatusDescription) + sizeofStatus
        Catch
            Return False
        End Try
        Return True
    End Function

    Public Function httpResponseLength(ByVal response As HttpListenerResponse) As Int64
        Dim rtn As Int64
        httpResponseLength(response, rtn)
        Return rtn
    End Function
#End If

    Public Function resolveAddressToHostname(ByVal ip As String, Optional ByVal noerrmsg As Boolean = False) As String
        Try
            Dim entry As IPHostEntry = Nothing
            entry = Dns.GetHostEntry(ip)
            If strsame(entry.HostName, ip, False) AndAlso _
               Not entry.Aliases Is Nothing AndAlso _
               entry.Aliases.Length() > 0 Then
                Return entry.Aliases(0)
            Else
                Return entry.HostName
            End If
        Catch ex As Exception
            If Not noerrmsg AndAlso isdebugmode() Then
                raiseError("cannot get host entry for ip " + ip + _
                           ", ex " + ex.Message + ", ignore.", errorHandle.errorType.warning)
            End If
            Return ip
        End Try
    End Function
End Module
